﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Markting;
using S4Store.Models;
using S4Store.Service;
using S4Store.Domain.NetMarketingKpi;

namespace S4Store.Controllers
{
    public class MarktingController : ReportBaseController
    {
        public ExhibitionReportService ExhibitionReportService { get; set; }
        public NetMarketingKpiService NetMarketingKpiService { get; set; }
        public NetMarketingKpiReportService NetMarketingKpiReportService { get; set; }
        public ReportService ReportService { get; set; }
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                t.Inject(NetMarketingKpiReportService);
                t.Inject(ReportService);
                model.ExhibitionReport = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                model.NetMarketingStoreReport = NetMarketingKpiReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                model.SellerReport = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id, Domain.EmployeeType.FieldSaler);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        #region 广宣计划
        public ActionResult GXJH()
        {
            GXJHModel model = new GXJHModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "GXJH";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult GXJHLS()
        {
            GXJHLSModel model = new GXJHLSModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "GXJHLS";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult GXJHHistory()
        {
            GXJHHistoryModel model = new GXJHHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ExhibitionReportService.GetExhibitionReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "GXJHHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
        #endregion

        #region 纸媒广告

        public ActionResult CTGG()
        {
            CTGGModel model = new CTGGModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CTGG";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult CTGGHistory()
        {
            CTGGHistoryModel model = new CTGGHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ExhibitionReportService.GetExhibitionReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CTGGHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
        #endregion

        #region 广宣质量

        public ActionResult GXZL()
        {
            GXZLModel model = new GXZLModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetTotalExhibitionReport(Month.Year, Month.Month, Store.Id);
                model.YearReport = ExhibitionReportService.GetExhibitionYearReport(Month.Year, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "GXZL";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult GXZLHistory()
        {
            GXZLHistoryModel model = new GXZLHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ExhibitionReportService.GetExhibitionReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "GXZLHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
        #endregion

        #region 活动计划
        public ActionResult HDJH()
        {
            HDJHModel model = new HDJHModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "HDJH";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ZTHD()
        {
            ZTHDModel model = new ZTHDModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ZTHD";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ZTHDHistory()
        {
            ZTHDHistoryModel model = new ZTHDHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ExhibitionReportService.GetExhibitionReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ZTHDHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
        #endregion

        #region 活动计划
        public ActionResult DTHD()
        {
            DTHDModel model = new DTHDModel();

            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                model.Report = ExhibitionReportService.GetExhibitionReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "DTHD";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult DTHDHistory()
        {
            DTHDHistoryModel model = new DTHDHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(ExhibitionReportService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ExhibitionReportService.GetExhibitionReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "DTHDHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            return View(model);
        }
        #endregion

        #region 网络媒体
        public ActionResult WLMT()
        {
            WLMTModel model = new WLMTModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                model.StoreReport = NetMarketingKpiReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.WebSite);
                foreach (var media in medias)
                {
                    var report = NetMarketingKpiReportService.GetMediaReport(Month.Year, Month.Month, Store.Id, media.Id);
                    model.MediaReports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "WLMT";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult WZXJB()
        {
            WZXJBModel model = new WZXJBModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.WebSite);
                foreach (var media in medias)
                {
                    var report = NetMarketingKpiReportService.GetMediaReport(Month.Year, Month.Month, Store.Id, media.Id);
                    model.MediaReports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "WZXJB";
            model.BarModel.Controller = "Markting";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("WZXJBHistory") + "' target='navtab' width='30%' title='网站集客效果历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult WZXJBHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            WZXJBHistoryModel model = new WZXJBHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.WebSite);
                model.Medias = medias.ToList();
                foreach (var media in medias)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = NetMarketingKpiReportService.GetMediaReport(dt.Year, dt.Month, Store.Id, media.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "WZXJBHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult JKXG(int MediaId)
        {
            JKXGModel model = new JKXGModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                model.Report = NetMarketingKpiReportService.GetMediaReport(Month.Year, Month.Month, Store.Id, MediaId);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "JKXG";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }


        public ActionResult JKXGHistory(int MediaId)
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            JKXGHistoryModel model = new JKXGHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var media = NetMarketingKpiService.GetMediaQuery().FirstOrDefault(x => x.Id == MediaId);
                model.Media = media;
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = NetMarketingKpiReportService.GetMediaReport(dt.Year, dt.Month, Store.Id, media.Id);
                    model.Reports.Add(report);
                }

                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "JKXGHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.PlusItem = "<input type='hidden' value='" + MediaId + "' name='MediaId'/>";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult YDMT()
        {
            YDMTModel model = new YDMTModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                model.Report = new Domain.NetMarketingKpiReport.NetMarketingMediaGroupReport(NetMarketingKpiReportService.GetStoreReport(Month.Year, Month.Month, Store.Id), NetMarketingMedia.MediaType.Mobile);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YDMT";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult YDXJB()
        {
            YDXJBModel model = new YDXJBModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.Mobile);
                foreach (var media in medias)
                {
                    var report = NetMarketingKpiReportService.GetMediaReport(Month.Year, Month.Month, Store.Id, media.Id);
                    model.MediaReports.Add(report);
                }
                model.GroupReport = new Domain.NetMarketingKpiReport.NetMarketingMediaGroupReport(NetMarketingKpiReportService.GetStoreReport(Month.Year, Month.Month, Store.Id), NetMarketingMedia.MediaType.Mobile);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YDXJB";
            model.BarModel.Controller = "Markting";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("YDXJBHistory") + "' target='navtab' width='30%' title='移动媒体性价比历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult YDXJBHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            YDXJBHistoryModel model = new YDXJBHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.Mobile);
                model.Medias = medias.ToList();
                foreach (var media in medias)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = NetMarketingKpiReportService.GetMediaReport(dt.Year, dt.Month, Store.Id, media.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YDXJBHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult YDJKXG()
        {
            YDJKXGModel model = new YDJKXGModel();

            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.Mobile);
                foreach (var media in medias)
                {
                    var report = NetMarketingKpiReportService.GetMediaReport(Month.Year, Month.Month, Store.Id, media.Id);
                    model.MediaReports.Add(report);
                }
                model.GroupReport = new Domain.NetMarketingKpiReport.NetMarketingMediaGroupReport(NetMarketingKpiReportService.GetStoreReport(Month.Year, Month.Month, Store.Id), NetMarketingMedia.MediaType.Mobile);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YDJKXG";
            model.BarModel.Controller = "Markting";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("YDJKXGHistory") + "' target='navtab' width='30%' title='移动媒体执行过程KPI历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult YDJKXGHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            YDXJBHistoryModel model = new YDXJBHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(NetMarketingKpiReportService);
                t.Inject(NetMarketingKpiService);
                var medias = NetMarketingKpiService.GetMedias(Store.Id).ToList().Where(x => x.Type == NetMarketingMedia.MediaType.Mobile);
                model.Medias = medias.ToList();
                foreach (var media in medias)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = NetMarketingKpiReportService.GetMediaReport(dt.Year, dt.Month, Store.Id, media.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YDXJBHistory";
            model.BarModel.Controller = "Markting";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        //public ActionResult WZZXGCHistory()
        //{
        //    WZZXGCHistoryModel model = new WZZXGCHistoryModel();
        //    if (Start == null)
        //    {
        //        Start = DateTime.Now.AddMonths(-6);
        //        End = DateTime.Now;
        //    }
        //    model.Start = Start.Value;
        //    model.End = End.Value;
        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiReportService);
        //        for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
        //        {
        //            var report = NetMarketingKpiReportService.GetNetMarketingKpiReport(dt.Year, dt.Month, Store.Id);
        //            model.Reports.Add(report);
        //        }
        //        t.Commit();
        //    }
        //    this.InjectReportModel(model);
        //    model.BarModel.Action = "WZZXGCHistory";
        //    model.BarModel.Controller = "Markting";
        //    model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
        //    return View(model);
        //}

        //public ActionResult WZZXKPI()
        //{
        //    WZZXKPIModel model = new WZZXKPIModel();

        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiReportService);
        //        model.Report = NetMarketingKpiReportService.GetNetMarketingKpiReport(Month.Year, Month.Month, Store.Id);
        //        t.Commit();
        //    }
        //    this.InjectReportModel(model);
        //    model.BarModel.Action = "WZZXKPI";
        //    model.BarModel.Controller = "Markting";
        //    model.BarModel.AddView(ReportBarModel.ViewItem.Month);
        //    return View(model);
        //}

        //public ActionResult WZZXKPIHistory()
        //{
        //    WZZXKPIHistoryModel model = new WZZXKPIHistoryModel();
        //    if (Start == null)
        //    {
        //        Start = DateTime.Now.AddMonths(-6);
        //        End = DateTime.Now;
        //    }
        //    model.Start = Start.Value;
        //    model.End = End.Value;
        //    using (var t = new Transtion())
        //    {
        //        t.Inject(NetMarketingKpiReportService);
        //        for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
        //        {
        //            var report = NetMarketingKpiReportService.GetNetMarketingKpiReport(dt.Year, dt.Month, Store.Id);
        //            model.Reports.Add(report);
        //        }
        //        t.Commit();
        //    }
        //    this.InjectReportModel(model);
        //    model.BarModel.Action = "WZZXKPIHistory";
        //    model.BarModel.Controller = "Markting";
        //    model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
        //    return View(model);
        //}
        #endregion
    }
}
